package org.ajc.gift.service.impl;


import org.ajc.gift.clients.AuthClient;
import org.ajc.gift.domain.Employee;
import org.ajc.gift.domain.LoginUser;
import org.ajc.gift.domain.Tenant;
import org.ajc.gift.domain.TenantMeal;
import org.ajc.gift.exception.BusinessException;
import org.ajc.gift.exception.ResponseCode;
import org.ajc.gift.exception.RonghuaAssert;
import org.ajc.gift.mapper.EmployeeMapper;
import org.ajc.gift.mapper.TenantMapper;
import org.ajc.gift.mapper.TenantMealMapper;
import org.ajc.gift.service.ITenantService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.ajc.gift.util.AjaxResult;
import org.ajc.gift.util.encrypt.MD5;
import org.ajc.gift.utils.BaiduAiUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ajc
 * @since 2024-03-23
 */
@Service
public class TenantServiceImpl extends ServiceImpl<TenantMapper, Tenant> implements ITenantService {

    @Autowired
    private TenantMapper tenantMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private AuthClient authClient;
    @Autowired
    private TenantMealMapper tenantMealMapper;

    @Transactional
    @Override
    public void settlement(Tenant tenant) {
        //1.判空校验JSR303
        //验证两次密码输入是否一致
        Employee admin = tenant.getAdmin();
        // 使用断言的工具类实现
        RonghuaAssert.isEq(admin.getPassword(),admin.getConfirmPassword(), ResponseCode.RESPONSE_CODE_400001);
        //2.验证当前的店铺,管理员信息是否已经注册过
        //根据name获取租户信息
        Tenant shopInDb = tenantMapper.queryByName(tenant.getCompanyName());
        // 使用断言的工具类实现
        RonghuaAssert.isNull(shopInDb,ResponseCode.RESPONSE_CODE_400002);
        //验证管理员,管理员信息是否已经注册过
        Employee employee = employeeMapper.queryByUsernameOrPhone(admin);
        // 使用断言的工具类实现
        RonghuaAssert.isNull(employee,ResponseCode.RESPONSE_CODE_400003);
        //3.验证(审核)店铺的名字,logo的合法性[涉政,涉爆,涉黄]
        // 使用断言的工具类实现
        RonghuaAssert.isTrue(BaiduAiUtils.textReview(tenant.getCompanyName()),ResponseCode.RESPONSE_CODE_400004);
        //4. 添加数据到数据库就OK
        //密码加盐加密
        String salt = UUID.randomUUID().toString().replace("-","");
        String password = MD5.getMD5("ajc"+salt+admin.getPassword()+"test");
        //先保存数据到t_user_login,并且要获取login_id
        LoginUser loginUser = new LoginUser();
        loginUser.setUsername(admin.getUsername());
        loginUser.setSalt(salt);
        loginUser.setPassword(password);
        loginUser.setTel(admin.getTel());
        loginUser.setType(1);
        AjaxResult ajaxResult = authClient.addOrUpdate(loginUser);
        //远程调用获取授权服务保存信息,有可能会报错不成功,如果报错应该抛出异常,防止程序继续往下执行
        if (!ajaxResult.isSuccess()){
            throw new BusinessException(ajaxResult.getMessage());
        }
        Long loginId = Long.parseLong(ajaxResult.getResultObj().toString());
        //设置状态码
        admin.setState(3);
        admin.setLoginId(loginId);
        employeeMapper.insert(admin);
        tenant.setRegisterTime(new Date());
        tenant.setAdminId(admin.getId());
        //获取套餐存放到中间表
        List<Long> mealIds = tenant.getMealIds();
        TenantMeal tenantMeal = new TenantMeal();
        for (Long id : mealIds){
            tenantMeal.setMealId(id);
            tenantMeal.setTenantId(tenant.getId());
            tenantMeal.setState(0);
            tenantMeal.setExpireDate(new Date());
            tenantMealMapper.insert(tenantMeal);
        }
    }
}
